package org.bugjail.servlet.action;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bugjail.data.Person;
import org.bugjail.data.TestDesign;
import org.bugjail.data.TestRound;
import org.bugjail.dataaccess.DAOFactory;
import org.bugjail.dataaccess.objects.TestCaseExecutionDAO;
import org.bugjail.servlet.BasicServlet;

/**
 * Servlet implementation class for Servlet: AddTestDesignToTestRoundServlet
 *
 */
 public class AddTestDesignToTestRoundServlet extends BasicServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public AddTestDesignToTestRoundServlet() {
		super();
	}

	@Override
	protected void action(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Person assignedUser = null;
		if (request.getParameter("assigneduser")!=null) {
			int userId = Integer.parseInt(request.getParameter("assigneduser"));
			assignedUser = DAOFactory.getDAOFactory().getPersonDAO().retrieveById(userId);
		}
		
		int testRoundId = Integer.parseInt(request.getParameter("testroundid"));
		TestRound tr = DAOFactory.getDAOFactory().getTestRoundDAO().retrieveById(testRoundId);
		String[] testdesignids = request.getParameterValues("checkid");
		List<TestDesign> testDesignList = new LinkedList<TestDesign>();
		int projectId = 0;
		for (String tstdid : testdesignids) {
			int id = Integer.parseInt(tstdid);
			TestDesign tstd = DAOFactory.getDAOFactory().getTestDesignDAO().retrieveById(id);
			testDesignList.add(tstd);
			projectId = tstd.getProject().getId();
		}
		TestCaseExecutionDAO dao = DAOFactory.getDAOFactory().getTestCaseExecutionDAO();
		dao.createBasedOnTestDesignList(testDesignList, tr, assignedUser);
		response.sendRedirect("TestDesignToTestRoundServlet?project="+projectId+"&testroundid="+tr.getId());
	}

	@Override
	protected void beforeAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}   	
}